<script setup lang="ts">
import { requiredCheck } from "@/utils/verify";
import { ref } from "vue";
import { getDataById, editData } from "@/api/modules/control-strategy/alarm-blocking";
import { ElMessage } from "element-plus";

interface Props {
  title?: string;
  rowData?: Object;
}

const props = withDefaults(defineProps<Props>(), {
  title: "",
  rowData: undefined
});
defineOptions({
  name: "UpdateForm" // 默认策略
});
import useUpdate from "@/hooks/useUpdate";
import { ifaceAll, ifacemodeUpdate } from "@/api/modules/dynamic-camouflage/line-config";

const emits = defineEmits(["success"]);
const { updateFormData, updateFormRef, updateFormRules, submit, showHandle, openHandle, closeHandle } = useUpdate({
  rules: {},
  defaultData: {
    block: [],
    blockWay: 3
  }
});
const ifaces = ref([]);

async function getIface() {
  try {
    const res = await ifaceAll();
    console.log(res);
    if (res) {
      ifaces.value = res;
    }
  } catch (error) {}
}

async function submitCb() {
  let sendId = props.rowData?.id;
  if (props.rowData?.ifname.includes("-")) {
    let ids = [];
    const attr = props.rowData?.ifname.split("-");
    for (const attrKey in ifaces.value) {
      if (attr.includes(ifaces.value[attrKey])) {
        ids.push(attrKey);
      }
    }
    sendId = ids.join("-");
  }
  await editData({
    description: "",
    id: sendId,
    plug_cfg: JSON.stringify({
      hpri_duration: "0",
      hpri_state: updateFormData.block.includes(3) ? "1" : "0",
      mpri_duration: "0",
      mpri_state: updateFormData.block.includes(2) ? "1" : "0",
      lpri_duration: "0",
      lpri_state: updateFormData.block.includes(1) ? "1" : "0",
      plug_class: String(updateFormData.blockWay),
      direction: "0",
      description: ""
    })
  });
  showHandle.value = false;
  ElMessage.success("操作成功");
  emits("success");
}

async function openHandleCb() {
  showHandle.value = true;
  const res = await getDataById(props.rowData?.id);
  console.log("openHandleCb::", res);
  let plugCfg = {};
  if (res.plug_cfg) {
    plugCfg = JSON.parse(res.plug_cfg);
  }
  let arr = [];
  if (Number(plugCfg.hpri_state) > 0) {
    arr.push(3);
  }
  if (Number(plugCfg.mpri_state) > 0) {
    arr.push(2);
  }
  if (Number(plugCfg.lpri_state) > 0) {
    arr.push(1);
  }
  updateFormData.block = arr;
  updateFormData.id = props.rowData?.id;
  updateFormData.blockWay = Number(plugCfg.plug_class) || 3;
}
</script>

<template>
  <div
    class="update-form mg-r-sm"
    style="display: inline-block"
    @click="
      openHandleCb();
      getIface();
    "
  >
    <slot>
      <el-button type="primary">新增</el-button>
    </slot>
  </div>
  <base-dialog @confirm="submitCb()" width="500px" :title="title" v-model="showHandle">
    <div class="details">
      <div class="details-main">
        <el-form ref="updateFormRef" :model="updateFormData" :rules="updateFormRules" label-width="120px" label-suffix="：">
          <el-form-item label="封堵" prop="name">
            <el-checkbox-group v-model="updateFormData.block">
              <el-checkbox :value="3" label="高危"></el-checkbox>
              <el-checkbox :value="2" label="中危"></el-checkbox>
              <el-checkbox :value="1" label="低危"></el-checkbox>
            </el-checkbox-group>
          </el-form-item>
          <el-form-item label="封堵方式" prop="name">
            <el-radio-group v-model="updateFormData.blockWay">
              <el-radio :value="1" label="串联"></el-radio>
              <!--              <el-radio :value="2" label="交换机"></el-radio>-->
<!--              <el-radio :value="3" label="第三方"></el-radio>-->
            </el-radio-group>
          </el-form-item>
        </el-form>
      </div>
    </div>
    <template #footer-more-button></template>
  </base-dialog>
</template>

<style lang="scss" scoped></style>
